import { loginApi } from "@/api/lot.js";

const state = {
  orgId: "",
  orgName: "",
};

const mutations = {
  SET_ORGID: (state, orgId) => {
    state.orgId = orgId;
  },
  SET_ORGNAME: (state, orgName) => {
    state.orgName = orgName;
  },
};

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, validate } = userInfo;

    return new Promise((resolve, reject) => {
      loginApi(userInfo)
        .then((response) => {
          // console.info("loginApi登录出参"+JSON.stringify(response))
          if (response.status == 200) {
            commit("SET_ORGID", response.obj.orgId);
            commit("SET_ORGNAME", response.obj.orgName);
          }
          resolve(response);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.sessionId)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
  // set menus
  setMenus({ commit }, menus) {
    return new Promise((resolve) => {
      console.log(menus, 283);
      commit("SET_MENUS", menus);
      resolve();
    });
  },

  // 获取验证码
  getcode(query) {
    return new Promise((resolve, reject) => {
      getcode(query)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  },
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
};
